/**
 * @title MatchListCommunicater.java
 * @package com.esun.cheetah.biz.match.matchlist
 * @description 比分列表全部比赛使用的通信类
 * @author liugan
 * @date 2012-3-29
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.match.matchlist;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.model.match.matchlist.Match;
import com.esun.cheetah.model.match.matchlist.Match.MatchState;
import com.esun.cheetah.model.match.matchlist.Match.ShowState;
import com.esun.cheetah.model.match.matchlist.Match.TimeState;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.log.EDebug;

/**
 * @title MatchListCommunicater
 * @description 比分列表全部比赛使用的通信类
 * @author liugan
 */
public class MatchListScoreCommunicater extends AbsInferiorityCommunicater {
	/**
	 * @field day
	 * @description 日期
	 */
	protected String day;

	protected String mRemoteDate;
	/**
	 * @field count
	 * @description 保存这一次通信的计算值
	 */
	protected int count;

	/**
	 * @field userName
	 * @description 用户名
	 */
	protected String userName;

	/**
	 * @field matchList
	 * @description 保存获得的比分列表
	 */
	protected ArrayList<Match> matchList;

	/**
	 * @param handler
	 */
	public MatchListScoreCommunicater(Handler handler) {
		super(handler);
	}

	public String getDay() {
		return day;
	}

	public MatchListScoreCommunicater(Handler handler, String day, int count) {
		this(handler);
		this.day = day;
		this.count = count;
		if(!TextUtils.isEmpty(UserInfo.getUserInfo().getCk())){
			this.userName = UserInfo.getUserInfo().getUserName();
		}
	}

	public ArrayList<Match> getMatchList() {
		return matchList;
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> hmJson = new HashMap<String, String>();
		hmJson.put(Key.KEY_ID, BizCode.BIZCODE_MATCH_LIST);
		hmJson.put(Key.KEY_LIST_TYPE, String.valueOf(Key.VALUE_ALL));
		if (!TextUtils.isEmpty(userName)) {
			hmJson.put(Key.KEY_USERNAME, userName);
		}
		return hmJson;
	}

	/**
	 * @description 将服务器返回的数据组装成List
	 */
	@Override
	public void getJsonContent(String serverRes) throws JSONException {
		EDebug.o(serverRes);
		JSONObject jsonObject = new JSONObject(serverRes);
		mRemoteDate = jsonObject.getString(Key.KEY_REMOTE_DATE);
		EDebug.o("----Time:" + mRemoteDate + " ----");
		JSONArray jsonArray = jsonObject.getJSONArray(Key.KEY_DATA);

		matchList = new ArrayList<Match>();
		// 循环访问每一场比赛的信息
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject matchObject = jsonArray.optJSONObject(i);
			EDebug.o(10, matchObject.toString());
			if (matchObject != null) {
				// 返回参数“d”：一场比赛的所有信息，以逗号分割
				// d="WEB对阵ID，对阵ID，比赛日期，比赛时间，
				// 联赛ID，联赛简体名，联赛繁体名，联赛颜色，赛季ID，
				// 主队ID，主队简体名，主队繁体名，主队排名，
				// 客队ID，客队简体名，客队繁体名，客队排名，
				// 当前轮次，阶段名，阶段模式，主队进球数，客队进球数，
				// 主队半场进球，客队半场进球，主队红牌，客队红牌，主队黄牌，客队黄牌"
				String description = (String) matchObject
						.get(Key.KEY_MATCH_DESCRIPTION);
				String[] descriptions = description.split(Language.COMMA);
				Match match = new Match();

				match.setMatchId(descriptions[1]);// 对阵ID
				match.setMatchDate(descriptions[2]);// 比赛日期
				match.setMatchTime(descriptions[3]); // 比赛开始时间
				match.setLeagueName(descriptions[5]);// 联赛名称
				match.setLeagueNameTraditional(descriptions[6]);
				match.setRound(descriptions[17]);// 当前轮次
				match.setStage(descriptions[18]);// 当前阶段
				
				match.setHomeName(descriptions[10]);// 主队名称 简体
				match.setHomeNameTraditional(descriptions[11]);// 主队名称 繁体
				match.setAwayName(descriptions[14]);// 客队名称 简体
				match.setAwayNameTraditional(descriptions[15]);// 客队名称 繁体	
							
 				match.setHomeNumber(descriptions[12]);// 主队排名		
				match.setAwayNumber(descriptions[16]);// 客队排名

				match.setHomeScore(Integer.parseInt(descriptions[20]));// 主队得分
				match.setAwayScore(Integer.parseInt(descriptions[21]));// 客队得分
				
				match.setDaxiaoqiu(matchObject.getString(Key.KEY_MATCH_ODDS));
				String peilvs = matchObject.getString(Key.KEY_MATCH_PEILV);
				if(!TextUtils.isEmpty(peilvs) && !",,".equals(peilvs)){
					String[] peilv = peilvs.split(",");
					if(peilv.length == 3){
						match.setWin(peilv[0]);
						match.setDraw(peilv[1]);
						match.setLose(peilv[2]);
					}
					
				}
				
				
				// 返回参数:是否关注
				// 1表示关注，0表示未关注
				int markStatus = matchObject.getInt(Key.KEY_MATCH_MARKED);
				if (markStatus == 1) {
					match.setMarked(true);
				} else {
					match.setMarked(false);
				}
				int status = matchObject.getInt(Key.KEY_MATCH_STATE);
				// 返回参数：比赛时间
				// 设置比赛的状态相关数据及分组
				setMatchState(match, status);
				
				match.setTiming(matchObject.getString(Key.KEY_MATCH_MATCH_TIME));
				
				matchList.add(match);
			}
		}

	}

	/**
	 * @title setMatchStateToday
	 * @description 设置比赛状态
	 * @param descriptions
	 * @param match
	 * @param status
	 */
	public static void setMatchState(Match match, int status) {
		switch (status) {
		case 0://未开赛
			match.setMatchState(MatchState.unplayed);
			match.setShowState(ShowState.unplayed);
			
			break;
		case 1://上半场
			match.setMatchState(MatchState.playing);
			match.setShowState(ShowState.playing);
			match.setTimeState(TimeState.firstHalf);
			
			break;
		case 2://中场
			match.setMatchState(MatchState.playing);
			match.setShowState(ShowState.playing);
			match.setTimeState(TimeState.halfTime);

			break;
		case 3://下半场
			match.setMatchState(MatchState.playing);
			match.setShowState(ShowState.playing);
			match.setTimeState(TimeState.secondHalf);
			
			break;
		case 4://已结束
			match.setMatchState(MatchState.over);
			match.setShowState(ShowState.over);
			break;
		case 5://取消
			match.setMatchState(MatchState.canceled);
			match.setShowState(ShowState.over);
			break;
		case 6://改期
			match.setMatchState(MatchState.changed);
			match.setShowState(ShowState.unplayed);
			break;
		case 7://腰斩
			match.setMatchState(MatchState.abort);
			match.setShowState(ShowState.over);
			break;
		case 8://中断
			match.setMatchState(MatchState.interrupt);
			match.setShowState(ShowState.playing);
			break;
		case 9://待定
			match.setMatchState(MatchState.undetermined);
			match.setShowState(ShowState.unplayed);

		}
	}

	@Override
	public void getServerResponseSuccess() {

		Message msg = handlerFromAct.obtainMessage();
//		if (MatchListManager.getInstance().addToAllMatchScoreListBuffer(
//				matchList, day, count)) {
			msg.what = Config.COMMUNICATER_SUCCESS;
//		} else {
//			msg.what = Config.COMMUNICATER_FAIL;
//		}

		msg.arg1 = count;
		//msg.obj = mRemoteDate;
		handlerFromAct.sendMessage(msg);

	}

	@Override
	public void getServerResponseFail(String serverRes) {
		Message msg = handlerFromAct.obtainMessage();
		msg.what = Config.COMMUNICATER_FAIL;
		msg.arg1 = count;
		handlerFromAct.sendMessage(msg);

	}

	@Override
	public void getServerResponseException() {
		Message msg = handlerFromAct.obtainMessage();
		msg.what = Config.COMMUNICATER_EXCEPTION;
		msg.arg1 = count;
		handlerFromAct.sendMessage(msg);

	}

}
